# uncompyle6 version 3.2.3
# Python bytecode 3.6 (3379)
# Decompiled from: Python 3.6.8 |Anaconda custom (64-bit)| (default, Feb 21 2019, 18:30:04) [MSC v.1916 64 bit (AMD64)]
# Embedded file name: helpers\exceptions\api_error_handlers.py
import sqlalchemy.exc, psycopg2
from server.http_responses.http_errors import Http500, ApiErrors


def handle_sqlalchemy_exc_operational_error(e):
    if "could not connect to server" in str(e):
        return Http500(
            message="Database connection error",
            code=ApiErrors.DB_CONNECTION_ERROR,
            details=["sqla.OperationalError", "could not connect"],
        )
    else:
        return Http500(
            message="Database connection error",
            code=ApiErrors.DB_CONNECTION_ERROR,
            details=["sqla.OperationalError"],
        )


def handle_psycopg2_operational_error(e):
    return Http500(
        message="Database connection error",
        code=ApiErrors.DB_CONNECTION_ERROR,
        details=["pg.OperationalError"],
    )


def handle_psycopg2_interface_error(e):
    return Http500(
        message="Database connection error",
        code=ApiErrors.DB_CONNECTION_ERROR,
        details=["pg.InterfaceError"],
    )


api_error_handlers = {
    sqlalchemy.exc.OperationalError: handle_sqlalchemy_exc_operational_error,
    psycopg2.OperationalError: handle_psycopg2_operational_error,
    psycopg2.InterfaceError: handle_psycopg2_interface_error,
}
